/**
 * 2009/12/09
 */
package com.googlecode.derby.customize.sample.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.googlecode.derby.customize.DataControllable;
import com.googlecode.derby.customize.DerbyDataController;
import com.googlecode.derby.customize.ResultData;
import com.googlecode.derby.customize.sample.entity.SamplePkOnlyEntity;

/**
 * @author osamu tanaka
 */
public final class SamplePkOnlyDao {

    /**
     * @param aDbName
     *            database name
     * @param user
     *            user
     * @param password
     *            password
     * @param key
     *            key
     * @return this
     */
    public static SamplePkOnlyDao getInstance(
            final String aDbName,
            final String user,
            final String password,
            final String key) {
        final SamplePkOnlyDao dao =
            new SamplePkOnlyDao(
                aDbName,
                user,
                password,
                key);
        return dao;
    }

    private final String dbName;
    private final DataControllable dc;

    private SamplePkOnlyDao(
            final String aDbName,
            final String user,
            final String password,
            final String key) {
        this.dc = DerbyDataController.getInstance(
                aDbName,
                user,
                password,
                key);
        this.dbName = aDbName;
    }

    /**
     * @param cndtn
     *            entity class
     * @return boolean
     */
    public boolean delete(final SamplePkOnlyEntity cndtn) {
        final List<SamplePkOnlyEntity> list =
            new ArrayList<SamplePkOnlyEntity>();
        list.add(cndtn);
        boolean ret = false;
        if (StringUtils.isNotEmpty(this.dbName)) {
            this.dc.table(SamplePkOnlyEntity.class).delete(list);
            if (this.dc.getExecuteUpdateCnt() == 1) {
                ret = true;
            }
            this.dc.disConnection();
        }
        return ret;
    }

    /**
     * @return List<ResultData>
     */
    public List<ResultData> findAll() {
        if (StringUtils.isNotEmpty(this.dbName)) {
            return this.dc
                    .select(SamplePkOnlyEntity.class).getResultDatas();
        }
        return null;
    }

    /**
     * @return dbName database name
     */
    public String getDbName() {
        return this.dbName;
    }

    /**
     * @param aClass
     *            entity class
     * @return true/false
     */
    public boolean isCreateTable(final Class<SamplePkOnlyEntity> aClass) {
        if (StringUtils.isNotEmpty(this.dbName)) {
            return this.dc
                    .table(aClass).isCreateTable();
        }
        return false;
    }

    /**
     * @param clause
     *            条件式
     * @return List<ResultData>
     */
    public List<ResultData> select(final String clause) {
        if (StringUtils.isNotEmpty(this.dbName)) {
            return this.dc
                    .select(SamplePkOnlyEntity.class)
                    .where(clause)
                    .getResultDatas();
        }
        return null;
    }

    /**
     * @param clause
     *            条件式
     * @return ResultData
     */
    public ResultData selectOne(final String clause) {
        if (StringUtils.isNotEmpty(this.dbName)) {
            final List<ResultData> results =
                this.dc
                        .select(SamplePkOnlyEntity.class)
                    .where(clause)
                    .getResultDatas();
            if ((results == null) || (results.size() != 1)) {
                return null;
            }
            return results.get(0);
        }
        return null;
    }

    /**
     * @param updt
     *            entity class
     * @return boolean
     */
    public boolean update(final SamplePkOnlyEntity updt) {
        final List<SamplePkOnlyEntity> list =
            new ArrayList<SamplePkOnlyEntity>();
        list.add(updt);
        boolean ret = false;
        if (StringUtils.isNotEmpty(this.dbName)) {
            DataControllable db = this.dc
                    .table(SamplePkOnlyEntity.class).insertUpdate(list);
            if (db.getExecuteUpdateCnt() == 1) {
                ret = true;
            }
            db.disConnection();
            db = null;
        }
        return ret;
    }

}
